package com.software.backend.login.service;

import com.software.backend.login.entity.User;
import com.software.backend.login.entity.UserExtends;
import com.software.backend.login.repository.AuthCodeRepositoryImpl;
import com.software.backend.login.repository.UserExtendsRepository;
import com.software.backend.login.repository.UsersRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpSession;
import java.util.Optional;

/**
 * @author 王俊坤
 * @version 1.0
 * @date 2022/11/10 17:10
 */

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    UsersRepository usersRepository;

    @Autowired
    UserExtendsRepository userExtendsRepository;

    @Autowired
    AuthCodeRepositoryImpl authCodeRepository;




    @Override
    public UserExtends loginByPhoneNumberAndPassword (String phoneNumber, String password){
        System.out.println(phoneNumber + " " + password);
        Optional<User> fondByPhone = usersRepository.findUserByPhone(phoneNumber);
        if (fondByPhone.isPresent()){
            User userBySelect = fondByPhone.get();
            if (userBySelect.getPassword().equals(password)){
                UserExtends userExtends = userExtendsRepository.findUserExtendsById(userBySelect.getId());
                return userExtends;
            }else {
                UserExtends passwordWrong = new UserExtends();
                passwordWrong.setId(null);
                return passwordWrong;
            }
        }else {
            return null;
        }
    }

    @Override
    public boolean createAndSendAuthCode(String phoneNumber, HttpSession session) {
        if (!authCodeRepository.hasCodeTimeOut(phoneNumber)){
            //获取随机数作为验证码
            String authCode = authCodeRepository.createAuthCode();
            //将验证码以一定时效保存
            authCodeRepository.saveAuthCode(authCode, session, phoneNumber);
            //调用第三方接口接口将验证码以短信形式发送
//            authCodeRepository.sendMessageToPhone(authCode, phoneNumber);
            System.out.println(authCode);
            return false;
        }else {
            return true;
        }
    }

    @Override
    public String isPhoneRegister(String phoneNumber) {
        boolean present = usersRepository.findUserByPhone(phoneNumber).isPresent();
        return present?"register":"login";
    }

    @Override
    public boolean varifyAuthCode(String phoneNumber, String authCode, HttpSession session) {
        return authCodeRepository.varifyAuthCode(authCode, phoneNumber, session);
    }

    @Override
    public UserExtends loginOrRegister(String phoneNumber, HttpSession session) {
        User user;
        UserExtends userExtends;
        Optional<User> usersByPhone = usersRepository.findUserByPhone(phoneNumber);
        if (usersByPhone.isPresent()){
            user = usersByPhone.get();
            userExtends = userExtendsRepository.findUserExtendsById(user.getId());
        }else {
            user = new User();
            user.setPhone(phoneNumber);
            User save = usersRepository.save(user);
            userExtends = new UserExtends();
            userExtends.setId(save.getId());
            userExtends.setName("用户" + String.valueOf((int)Math.random() * 1000000 + 1000000).substring(1));
            userExtends.setGender("0");
            userExtends.setAvatarUrl("");
            userExtends.setLikeCount(0L);
            userExtends.setFollowerCount(0L);
            userExtends.setPostCount(0L);
            userExtends.setAttentionCount(0L);
            userExtends.setIntroduction("");
            userExtendsRepository.save(userExtends);
        }
        session.removeAttribute("isLogin");
        session.setAttribute("isLogin", true);
        session.removeAttribute("loginId");
        session.setAttribute("loginId", user.getId());
        return userExtends;
    }


}
